package com.bmf.module.examination.controller;


import cn.hutool.core.util.ObjectUtil;
import com.bmf.constant.AdminResultEnum;
import com.bmf.core.result.PageResponse;
import com.bmf.module.examination.entity.Examination;
import com.bmf.module.examination.service.ExaminationService;
import com.bmf.module.examination.vo.*;
import com.bmf.utils.BeanCopyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.Serializable;
import java.util.List;
import com.bmf.core.result.R;


/**
 * (Examination)表控制层
 *
 * @author makejava
 * @since 2022-11-29 17:54:33
 */
@RestController
@RequestMapping("examination")
@Api(tags = {"试卷接口"})
public class ExaminationController{
    /**
     * 服务对象
     */
    @Autowired
    private ExaminationService examinationService;

    /**
     * 分页查询所有试卷
     *
     * @return 所有数据
     */
    @PostMapping("/selectAll")
    @ApiOperation(value = "分页查询所有试卷")
    public R<PageResponse<ExaminationResultVo>> selectAll(@RequestBody ExaminationPageVo examinationPageVo) {
        return R.success(examinationService.selectAll(examinationPageVo));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ApiOperation(value = "通过主键查询试卷")
    public R<ExaminationResultVo> selectOne(@PathVariable Serializable id) {
        Examination byId = examinationService.getById(id);
        if(ObjectUtil.isEmpty(byId)){
            return R.success(null);
        }
        return R.success(BeanCopyUtils.copyBean(examinationService.getById(id), ExaminationResultVo.class));
    }

    /**
     * 新增试卷
     *
     * @param examinationCreateVo 实体对象
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation(value = "新增试卷")
    public R<Integer> insert(@RequestBody ExaminationCreateVo examinationCreateVo) {
        return R.success(examinationService.insert(examinationCreateVo));
    }

    /**
     * 修改数据
     *
     * @param updateVo 实体对象
     * @return 修改结果
     */
    @PutMapping
    @ApiOperation(value = "修改试卷")
    public R<Void> update(@RequestBody ExaminationUpdateVo updateVo) {
        boolean b = examinationService.updateById(BeanCopyUtils.copyBean(updateVo, Examination.class));
        AdminResultEnum.UPDATE_EXCEPTION.assertIsTrue(b);
        return R.ok();
    }

    /**
     * 删除数据
     *
     * @param ids 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    @ApiOperation(value = "删除试卷")
    public R<Void> delete(@RequestBody List<Integer> ids) {
        examinationService.deleteByIds(ids);
        return R.ok();
    }

    /**
     * 预览试卷
     * @param id
     * @return
     */
    @GetMapping("/selectExamination/{id}")
    @ApiOperation(value = "预览试卷")
    public R<List<BigQuestionTestPaperResultVo>> selectExamination(@PathVariable("id") Integer id) {
        return R.success(examinationService.getExamination(id));
    }
}

